package com.zhongxi.guoyuan.view;

import android.os.Build;
import android.view.View;
import android.webkit.WebView;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.google.gson.Gson;
import com.zhongxi.guoyuan.R;
import com.zhongxi.guoyuan.application.MyApplication;
import com.zhongxi.guoyuan.base.BaseActivity;
import com.zhongxi.guoyuan.entity.gift.GiftRuleResult;
import com.zhongxi.guoyuan.url.Url;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

public class HtmlActivity extends BaseActivity implements View.OnClickListener {

    private String content;
    private WebView webView;
    private String type;
    private ImageView mImageView;
    private GiftRuleResult ruleResult;

    @Override
    protected void initView() {
        super.initView();
        setContentView(R.layout.activity_html);
        initTitle();
        content=getIntent().getStringExtra("content");
        type = getIntent().getStringExtra("type");

        webView = (WebView) findViewById(R.id.webView);
        mImageView= (ImageView) findViewById(R.id.imageView);

        if (type.equals("activityRule")) {
            tvTitle.setText("团购秒杀规则");
        } else if (type.equals("doudouRule")) {
            tvTitle.setText("豆豆说明");
            getContentData();
        }else if (type.equals("consumeRule")){
            tvTitle.setText("消费红包");
        }

        initWebView();
    }

    private void getContentData() {
            getServer(Url.CONSUME_RULE, null, "", false, null, "get");
    }

    private void initWebView() {
        webView.getSettings().setJavaScriptEnabled(true);
        webView.getSettings().setBuiltInZoomControls(false);
        webView.getSettings().setDisplayZoomControls(false);
        webView.getSettings().setDefaultTextEncodingName("UTF-8");
        webView.getSettings().setBlockNetworkImage(false);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            webView.getSettings().setMixedContentMode(webView.getSettings().MIXED_CONTENT_ALWAYS_ALLOW);  //注意安卓5.0以上的权限
        }
    }

    @Override
    protected void initData() {
        super.initData();
        if (type.equals("activityRule")){
            webView.loadDataWithBaseURL(null, getNewContent(content), "text/html", "utf-8", null);
        }else if (type.equals("consumeRule")){
            Glide.with(this).load(Url.RED_PACKET).fitCenter().into(mImageView);
        }

    }

    @Override
    protected void processSuccessResult(String response) {
        super.processSuccessResult(response);
        Gson gson = new Gson();
        ruleResult = gson.fromJson(response, GiftRuleResult.class);
        if (null != ruleResult.getSafeobj() && !"".equals(ruleResult.getSafeobj())) {
            MyApplication.getInstance().setSpValue(ruleResult.getSafeobj());
        }
        if (null != ruleResult.getData().getDescription() && !"".equals(ruleResult.getData().getDescription())) {

            webView.loadDataWithBaseURL(null, getNewContent(ruleResult.getData().getDescription()), "text/html", "utf-8", null);

        }
    }

    @Override
    protected void ProcessFailResult(String response) {
        super.ProcessFailResult(response);
    }

    private String getNewContent(String htmltext) {

        Document doc = Jsoup.parse(htmltext);
        Elements elements = doc.getElementsByTag("img");
        for (Element element : elements) {
            element.attr("width", "100%").attr("height", "auto");
        }

        return doc.toString();
    }

    @Override
    protected void initEvent() {
        super.initEvent();
        ivBack.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.ivBack:
                finish();
                break;
        }
    }
}
